package com.iwomy.secureplat.platform.transmission.parse;

import com.iwomy.secureplat.common.core.util.SpringContextHolder;
import com.iwomy.secureplat.platform.entity.BasicDeviceEntity;
import com.iwomy.secureplat.platform.service.AlarmBaseService;
import com.iwomy.secureplat.platform.service.BasicDeviceService;
import com.iwomy.secureplat.platform.service.DeviceLogService;
import com.iwomy.secureplat.platform.transmission.DrcProInfo;
import com.iwomy.secureplat.platform.transmission.entity.SensorTransmissionData;
import io.netty.buffer.ByteBuf;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Date;
import java.util.List;

/**
 * 解析接口
 *
 * @author yangze
 */
public interface ParseData extends AlarmBaseService {

	/**
	 * 解析
	 *
	 * @param bytes
	 * @return 是否解析成功
	 */
	SensorTransmissionData parse(byte[] bytes, BasicDeviceEntity device);

	//解码
	boolean decoder(ByteBuf in, List<Object> list);

	/**
	 * 保存
	 *
	 * @return 是否报错成功
	 */
	boolean save(Boolean updateDeviceState,BasicDeviceEntity basicDeviceEntity, SensorTransmissionData transmissionData);

	//保存文本
	boolean alarm(BasicDeviceEntity device, SensorTransmissionData transmissionData);

	//设置项目信息
	void setDrcProInfo(DrcProInfo drcProInfo);

	//获取项目信息
	DrcProInfo getDrcProInfo();

	//是否是心跳包
	boolean isHeartPack();

	//获取最后心跳时间
	Date getLastHeartPackDate();

	/**
	 * 整数转十六进制
	 *
	 * @param b
	 * @return
	 */
	default int byte2Hex(byte b) {
		return b & 0XFF;
	}

	/**
	 * 正常复制
	 */
	default Integer copyAndMoveReadIndex(byte[] src, Integer readIndex, byte[] target) {
//        System.out.println("当前索引:" + readIndex);
		System.arraycopy(src, readIndex, target, 0, target.length);
		return readIndex += target.length;
	}

	/**
	 * 大小端转换
	 */
	default Integer copyAndMoveReadIndexOrder(byte[] src, Integer readIndex, byte[] target) {
//        System.out.println("当前索引:" + readIndex);
		System.arraycopy(src, readIndex, target, 0, target.length);
		target = ByteBuffer.wrap(target).order(ByteOrder.LITTLE_ENDIAN).array();
		return readIndex += target.length;
	}

	/**
	 * 保存设备报文
	 */
	DeviceLogService deviceLogService = SpringContextHolder.getBean(DeviceLogService.class);

	BasicDeviceService basicDeviceService = SpringContextHolder.getBean(BasicDeviceService.class);
}